﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.Core.Repository.Customer;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Customer;
using WaterRevenueMS.Domain.ViewModels.DataAuditing;
using WaterRevenueMS.Core.Repository.Charge;
using WaterRevenueMS.Componnent.Entities.Charge;
using System.Linq.Expressions;
using WaterRevenueMS.Core.Repository.BasicSet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.Core.Repository.ReadMeterMS;
using WaterRevenueMS.Componnent.Entities.ReadMeterMS;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IMeterInfoService))]
    public class MeterInfoService : CoreServiceBase, IMeterInfoService
    {
        [Import]
        public IMeterInfoRepository MeterInfoRepository { get; set; }
        [Import]
        public IChargeRecordRepository ChargeRecordRepository { get; set; }

        [Import]
        private ICustomerRepository CustomerRepository { get; set; }

        [Import]
        public IRuleOfCodeRepository RuleOfCodeRepository { get; set; }


        public IQueryable<Componnent.Entities.Customer.MeterInfo> MeterInfos
        {
            get { return MeterInfoRepository.Entities; }
        }

        public OperationResult Insert(Domain.ViewModels.Customer.MeterInfo.MeterInfoModel model)
        {
            GenerateMeterCode(model);
            var entity = new MeterInfo
            {
                Yong_HSZ_No = model.Yong_HSZ_No!=0?model.Yong_HSZ_No:null,
                Shou_FFS_No = model.Shou_FFS_No!=0?model.Shou_FFS_No:null,
                Yong_SLX_No = model.Yong_SLX_No!=0?model.Yong_SLX_No:null,
                Yi_BLX_No = model.Yi_BLX_No!=0?model.Yi_BLX_No:null,
                Zhong_JLX_No = model.Zhong_JLX_No!=0?model.Zhong_JLX_No:null,
                Zhong_JDZ = model.Zhong_JDZ,
                Zhong_JTD = model.Zhong_JTD,
                Ji_ZQLX_No = model.Ji_ZQLX_No!=0?model.Ji_ZQLX_No:null,
                Ji_ZQDZ = model.Ji_ZQDZ,
                Ji_ZQTD = model.Ji_ZQTD,
                Shou_CQLX_No = model.Shou_CQLX_No!=0?model.Shou_CQLX_No:null,
                Shui_BCS_No = model.Shui_BCS_No!=0?model.Shui_BCS_No:null,
                Shui_BKJ_No = model.Shui_BKJ_No!=0?model.Shui_BKJ_No:null,
                Biao_CSZ_No = model.Biao_CSZ_No!=0?model.Biao_CSZ_No:null,
                Yi_BMC = model.Yi_BMC,
                Yi_BBH = model.Yi_BBH,
                Yi_BDZ = model.Yi_BDZ,
                Yi_BDD = model.Yi_BDD,
                Bei_L = model.Bei_L,
                Qian_FH = model.Qian_FH,
                An_ZWZ = model.An_ZWZ,
                Qi_YRQ = model.Qi_YDT
            };
            MeterInfoRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(Domain.ViewModels.Customer.MeterInfo.MeterInfoModel model)
        {
            var entity = new MeterInfo
            {
                Id=model.Id,
                Yong_HSZ_No = model.Yong_HSZ_No!=0?model.Yong_HSZ_No:null,
                Shou_FFS_No = model.Shou_FFS_No!=0?model.Shou_FFS_No:null,
                Yong_SLX_No = model.Yong_SLX_No!=0?model.Yong_SLX_No:null,
                Biao_CSZ_No = model.Biao_CSZ_No!=0?model.Biao_CSZ_No:null,
                Yi_BLX_No = model.Yi_BLX_No!=0?model.Yi_BLX_No:null,
                Zhong_JLX_No = model.Zhong_JLX_No != 0 ? model.Zhong_JLX_No : null,
                Zhong_JDZ = model.Zhong_JDZ,
                Zhong_JTD = model.Zhong_JTD,
                Ji_ZQLX_No = model.Ji_ZQLX_No != 0 ? model.Ji_ZQLX_No : null,
                Ji_ZQDZ = model.Ji_ZQDZ,
                Ji_ZQTD = model.Ji_ZQTD,
                Shou_CQLX_No = model.Shou_CQLX_No != 0 ? model.Shou_CQLX_No : null,
                Shui_BCS_No = model.Shui_BCS_No != 0 ? model.Shui_BCS_No : null,
                Shui_BKJ_No = model.Shui_BKJ_No != 0 ? model.Shui_BKJ_No : null,
                Yi_BMC = model.Yi_BMC,
                Yi_BBH = model.Yi_BBH,
                Yi_BDZ = model.Yi_BDZ,
                Yi_BDD = model.Yi_BDD,
                Bei_L = model.Bei_L,
                Qian_FH = model.Qian_FH,
                An_ZWZ = model.An_ZWZ,
                Qi_YRQ = model.Qi_YDT
            };
            MeterInfoRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public Componnent.Common.OperationResult Delete(int Id)
        {
            var entity = MeterInfoRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                MeterInfoRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "添加成功");
        }


        public OperationResult Update(MeterInfo meterInfo)
        {
            MeterInfoRepository.Update(meterInfo);
            return new OperationResult(OperationResultType.Success, "成功");
        }

        public OperationResult Update(DataAuditingModel model)
        {
            var entity = MeterInfoRepository.Entities.FirstOrDefault(t => t.Id == model.Id);
            if (model.IsWaterCharge)
            {
                entity.Fei_YTZ = model.Fei_YTX;                
                WaterUseChange waterUseChange = new WaterUseChange()
                {
                    MeterNo=entity.Id,
                    Shui_L=entity.Fei_YTZ,
                    CreateDT=DateTime.Now,
                    IsDeleted=false
                };
                entity.WaterUseChange.Add(waterUseChange);
                
            }
            else
            {
                WaterChargeChange waterChargeChange = new WaterChargeChange()
                {
                    MeterNo=entity.Id,
                    Fei_Y=entity.Fei_YTZ,
                    CreateDT=DateTime.Now,
                    IsDeleted=false
                };
                entity.WaterChargeChange.Add(waterChargeChange);
                entity.Yong_LTZ = model.Yong_LBC;
            }
            MeterInfoRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "调整成功");
        }

        public OperationResult ChargeSettlement(Expression<Func<MeterInfo, Boolean>> expr)
        {
            var meters = MeterInfoRepository.Entities.Where(expr);
            foreach (var item in meters)
            {
                item.Jie_SDT = DateTime.Now;
                item.Shi_FJS = 1;
                item.Yong_LBC = item.Du_SBC - item.Du_SBQ;
                item.Yong_LBQ = item.Du_SBQ - item.Du_SSQ;
                item.Du_SSQ = item.Du_SBQ;
                item.Du_SBQ = item.Du_SBC;
                item.Ji_FBC = GenerateFee(item,item.Yong_LBC + item.Yong_LTZ);
                item.Jie_SDT = DateTime.Now;
                item.Customer.ChargeRecord.Add(new ChargeRecord {
                    Ben_QDS = item.Du_SBQ,
                    Shang_QDS = item.Du_SSQ,
                    Ben_QYL = item.Yong_LBC,
                    Ben_CJF = item.Ji_FBC,
                    Yong_SLB=(int)item.Yong_SLX_No,
                    Jiao_FZT=1,
                    CreateDT=DateTime.Now,
                    ReviseDT=DateTime.Now
                });
                item.RMeter_No = null;
                MeterInfoRepository.Update(item, false);         
            }
            UnitOfWork.Commit();
            return new OperationResult(OperationResultType.Success, "结算成功");
        }

        //生成用户编号
        private void GenerateMeterCode(Domain.ViewModels.Customer.MeterInfo.MeterInfoModel model)
        {
            var entity = RuleOfCodeRepository.Entities.FirstOrDefault(t => t.Bian_MDX == 2);
            var customer = CustomerRepository.Entities.FirstOrDefault(t => t.Id == model.Yong_HSZ_No);
            long maxCode = entity.Auto_Num;
            string Zi_ZBHCD = "{0:" + string.Empty.PadRight(entity.Zi_ZBHCD, '0') + "}";
            switch (model.RuleOfCode)
            {
                case 0:
                    break;
                case 1:
                    model.Yi_BBH = customer.Yong_HBH;
                    break;
                case 2:
                    model.Yi_BBH = customer.Yong_HBH + string.Format(Zi_ZBHCD, (maxCode + entity.Zi_ZBHZL));
                    entity.Auto_Num = entity.Auto_Num + entity.Zi_ZBHZL;
                    break;
            }

            RuleOfCodeRepository.Update(entity);
        }

        //生成水费
        private float GenerateFee(MeterInfo info,float total)
        {
            var kindOfuse = info.KindOfWaterUse;
            float fee = 0;
            switch (kindOfuse.Jie_TSZ)
            {
                case 0://单价计算
                    fee = total * kindOfuse.Yong_SDJ;
                    
                    break;
                case 1://2阶计算
                    if (total > kindOfuse.Yong_SL)
                    {
                        fee=kindOfuse.Yong_SL*kindOfuse.Yong_SDJ+(total-kindOfuse.Yong_SL)*kindOfuse.Yong_SDJA;
                    }
                    else
                    {
                        fee = total * kindOfuse.Yong_SDJ;
                    }
                    break;
                case 2:
                    if (total > kindOfuse.Yong_SLA)
                    {
                        fee = kindOfuse.Yong_SL * kindOfuse.Yong_SDJ;
                        fee += (kindOfuse.Yong_SLA - kindOfuse.Yong_SL) * kindOfuse.Yong_SDJA;
                        fee += (total - kindOfuse.Yong_SLA) * kindOfuse.Yong_SDJB;
                    }
                    else if (total > kindOfuse.Yong_SL)
                    {
                        fee = kindOfuse.Yong_SL * kindOfuse.Yong_SDJ;
                        fee +=(total-kindOfuse.Yong_SL)*kindOfuse.Yong_SDJA;
                    }
                    else
                    {
                        fee = total * kindOfuse.Yong_SDJ;
                    }
                    break;
                case 3:
                    if (total > kindOfuse.Yong_SLB)
                    {
                        fee = kindOfuse.Yong_SL * kindOfuse.Yong_SDJ;
                        fee += (kindOfuse.Yong_SLA - kindOfuse.Yong_SL) * kindOfuse.Yong_SDJA;
                        fee += (kindOfuse.Yong_SLB - kindOfuse.Yong_SLA) * kindOfuse.Yong_SDJB;
                        fee += (total - kindOfuse.Yong_SLB) * kindOfuse.Yong_SDJC;
                    }
                    else if (total > kindOfuse.Yong_SLA)
                    {
                        fee = kindOfuse.Yong_SL * kindOfuse.Yong_SDJ;
                        fee += (kindOfuse.Yong_SLA - kindOfuse.Yong_SL) * kindOfuse.Yong_SDJA;
                        fee += (total - kindOfuse.Yong_SLA) * kindOfuse.Yong_SDJB;
                    }
                    else if (total > kindOfuse.Yong_SL)
                    {
                        fee = kindOfuse.Yong_SL * kindOfuse.Yong_SDJ;
                        fee += (total - kindOfuse.Yong_SL) * kindOfuse.Yong_SDJA;
                    }
                    else
                    {
                        fee = total * kindOfuse.Yong_SDJ;
                    }
                    break;
            }
            fee += GenerateAddItemFee(kindOfuse, total);
            return fee;
        }
        //生成附加费用
        private float GenerateAddItemFee(KindOfWaterUse waterUse,float total)
        {
            if (waterUse.AdditionItem != null)
            {
                float sum = 0;
                foreach (AdditionItem item in waterUse.AdditionItem)
                {
                    sum += item.Fu_JFDJ * total;
                }
                return sum;
            }
            return 0;
        }
    }
}
